<template>
	<view class="">
		<titleBar title="我的" :isSubPage="false"></titleBar>
		<view class="page-container">

			<!-- 个人信息 -->
			<view class="info-container" @click="navigateToUserDetail">
				<view class="info-item">
					<image class="avatar" :src="userAvatar" mode="aspectFill"></image>
					<view class="user-info">
						<text class="name">{{userName}}</text>
						<text class="signature">{{introduce}}</text>
					</view>
				</view>
			</view>

			<!-- 宠物信息 -->
			<view class="info-container-pet" @click="navigateToPetDetail">
				<view class="info-item">
					<image class="avatar" :src="petAvatar" mode="aspectFill"></image>
					<view class="pet-info">
						<text class="name">{{petName}}</text>
						<view class="pet-details">
							<text>种类：{{petType}}</text>
							<text>体重：{{petWeight}}</text>
							<text>年龄：{{petAge}}</text>
						</view>
					</view>
				</view>
			</view>

			<!-- 通用设置列表 -->
			<view class="settings-container">
				<view class="setting-item">主题设置</view>
				<view class="setting-item">意见反馈</view>
				<view class="setting-item">修改密码</view>
				<view class="setting-item">关于我们</view>
			</view>
			<view class="danger-zone">
				<button class="logout-btn" @click="logout">退出登录</button>
			</view>
		</view>

	</view>

</template>

<script>
	import titleBar from '@/components/titleBar.vue'
	import {
		userApi
	} from '@/api/user.js'

	export default {
		components: {
			titleBar
		},
		data() {
			return {
				userName: '',
				userAvatar: '',
				introduce: '',
				petName: '',
				petAvatar: '',
				petType: '',
				petWeight: '',
				petAge: '',
				userId: '',
			};
		},
		mounted() {
			this.userId = uni.getStorageSync('userId')
			this.getUserInfo()
			this.getPetInfo()
		},
		methods: {
			navigateToUserDetail() {
				uni.redirectTo({
					url: '/pages/mine/mineInfo'
				})
			},
			navigateToPetDetail() {
				uni.redirectTo({
					url: '/pages/mine/petInfo'
				})
			},
			async getUserInfo() {
				try {
					const result = await userApi.getUserInfo()
					this.userName = result.username
					this.userAvatar = result.avatar
					this.introduce = result.introduce || '这个人很懒，什么都没有留下~'
					console.log('获取用户信息成功：', this.userAvatar);
				} catch (error) {
					uni.showToast({
						title: '获取用户信息失败: ' + (error.message || '未知错误'),
						icon: 'none'
					});
					console.error('获取用户信息失败：', error);
				}
			},
			async getPetInfo() {
				try {
					const result = await userApi.getPetInfo({
						userId: this.userId
					})
					if (result) {
						this.petName = result.petName
						this.petAvatar = result.avatar
						this.petType = result.petType
						this.petWeight = result.petWeight
						this.petAge = result.petAge
					} else {
						this.petName = '快去完善宠物信息吧~'
						this.petAvatar = ''
						this.petType = '快去完善宠物信息吧~'
						this.petWeight = '快去完善宠物信息吧~'
						this.petAge = '快去完善宠物信息吧~'
					}
					console.log('获取宠物信息成功：', result);
				} catch (error) {
					uni.showToast({
						title: '获取宠物信息失败: ' + (error.message || '未知错误'),
						icon: 'none'
					});
					console.error('获取宠物信息失败：', error);
				}
			},
			async logout() {
				uni.showModal({
					title: '提示',
					content: '确定要退出登录吗？',
					success: (res) => {
						if (res.confirm) {
							try {
								const result = userApi.logout()
								console.log('退出登录成功：', result);
							} catch (error) {
								console.error('退出登录失败：', error);
							}
							// 清除登录状态
							uni.removeStorageSync('token');
							// 返回登录页
							uni.reLaunch({
								url: '/pages/login/login'
							});
						}
					}
				});
			},
		}
	};
</script>

<style scoped>
	.page-container {
		padding: 40rpx;
		background-color: #fff;
	}

	.info-container {
		margin-bottom: 40rpx;
		background-color: white;
		border-radius: 20rpx;
		padding: 40rpx;
		box-shadow: 0 16rpx 40rpx rgba(0, 0, 0, 0.1);
		margin-top: 30rpx;
	}

	.info-container-pet {
		margin-bottom: 40rpx;
		background-color: white;
		border-radius: 20rpx;
		padding: 40rpx;
		box-shadow: 0 16rpx 40rpx rgba(0, 0, 0, 0.1);
	}

	.info-item {
		display: flex;
		flex-direction: row;
		align-items: center;
	}

	.avatar {
		width: 130rpx;
		height: 130rpx;
		border-radius: 100%;
		margin-right: 30rpx;
	}

	.name {
		font-size: 36rpx;
		font-weight: bold;
		margin-bottom: 10rpx;
	}

	.user-info {
		display: flex;
		flex-direction: column;
	}

	.signature {
		font-size: 28rpx;
		color: #666;
	}

	.pet-info {
		display: flex;
		flex-direction: column;
		width: 50%;
	}

	.pet-details {
		display: flex;
		flex-wrap: wrap;
		font-size: 28rpx;
		color: #666;
	}

	.pet-details text {
		margin-right: 20rpx;
	}

	.settings-container {
		background-color: white;
		border-radius: 20rpx;
		padding: 40rpx;
		box-shadow: 0 16rpx 40rpx rgba(0, 0, 0, 0.1);
	}

	.setting-item {
		padding: 20rpx 0;
		border-bottom: 2rpx solid rgba(0, 0, 0, 0.1);
		font-size: 32rpx;
	}

	.setting-item:last-child {
		border-bottom: none;
	}

	.danger-zone {
		margin-top: 60rpx;
	}

	.logout-btn {
		width: 70%;
		height: 90rpx;
		background-color: #FF6B35;
		color: white;
		border-radius: 45rpx;
		font-size: 30rpx;
		margin-bottom: 30rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}
</style>